<?php
/* @var $list \common\models\Package[] */
$this->title = '购买会员'
?>
	   	<section class="app-container app-form app-vip">
         
            <?= $this->render('flash') ?>
            <!-- form -->
            <div class="app-form-checkbox">
                <?php
                    foreach($list as $k => $model){
                        echo '<label for="">';
                        echo '<input type="radio" name="package" value="'. $model->package_id .'">';
                        echo '<span>'. $model->package_name .'</span>';
            	        echo  '</label>';
                    }
                ?>
            </div>

            <div class="app-form-content">
                
                <?php
                    foreach($list as $k => $model ){
                        $price = \Yii::$app->user->identity->getPackageGroup();
                        if( $k === 0 ){
                            echo '<div class="app-form-tab-content active">';
                        }else{
                            echo '<div class="app-form-tab-content">';
                        }
            		    echo '<span>'. $model->package_name .'： <em>'. $model->$price .'</em> 元</span>';
                        foreach($model->goods as $k => $v){
                            echo '<p>';
                            echo $v['goods_name'].' 一次性补贴：'. $v['sale'] .'%';
                            echo '</p>';
                        }
            	        echo '</div>';
                    }
                ?>

            </div>

            <div class="app-form-button">
                <button class="app-form-control app-btn-submit app-btn-small">去支付</button>
            </div>

        </section>

        <section class="app-mask"></section>
<script language="javascript">
    function jsApiCall(appId,timeStamp,nonceStr,package,signType,paySign)
    {
        WeixinJSBridge.invoke(
            'getBrandWCPayRequest', {
                "appId" : appId,     //公众号名称，由商户传入
                "timeStamp": timeStamp,         //时间戳，自1970年以来的秒数
                "nonceStr" : nonceStr, //随机串
                "package" : package,
                "signType" : signType,         //微信签名方式:
                "paySign" : paySign //微信签名
            },
            function(res){
                WeixinJSBridge.log(res.err_msg);
                if(res.err_msg == "get_brand_wcpay_request:ok" ) {
                    window.location.href = "<?= \yii\helpers\Url::toRoute(['member/index']) ?>";
                }else if(res.err_msg == "get_brand_wcpay_request:cancel"){
                    layer.open({
                        btn: ['确定'],content:'您取消了支付！',shade:true,shadeClose:false})
                }else if(res.err_msg == "get_brand_wcpay_request:fail"){
                    layer.open({btn: ['确定'],content:'支付失败',shade:true,shadeClose:false})
                }
            }
        );
    }
    
    function callpay(appId,timeStamp,nonceStr,package,signType,paySign)
    {
        if (typeof WeixinJSBridge == "undefined"){
            if( document.addEventListener ){
                document.addEventListener('WeixinJSBridgeReady', jsApiCall, false);
            }else if (document.attachEvent){
                document.attachEvent('WeixinJSBridgeReady', jsApiCall);
                document.attachEvent('onWeixinJSBridgeReady', jsApiCall);
            }
        }else{
            jsApiCall(appId,timeStamp,nonceStr,package,signType,paySign);
        }
    }
</script>
<script src="/js/jquery.min.js"></script>
<script src="/js/layer_mobile/layer.js"></script>
    <script>
        $(function(){
            $('.app-form-checkbox label').on('click',function(){
            	var _this = $(this), _index = _this.index();

            	$('.app-form-tab-content').eq(_index).fadeIn().siblings().hide();
            })
            
            $('.app-btn-submit').click(function(){
                var package_id = $('input[name="package"]:checked').val();
                if( package_id <= 0 ) return false;
                $.post("<?= \yii\helpers\Url::toRoute(['member/do-buy']) ?>", {"_csrf-frontend":"<?= Yii::$app->request->csrfToken ?>", package_id:package_id}, function(result){
                    layer.open({type: 2, shadeClose: false});
                    if( result.success == true ){
                        var data = result.data;
                        layer.closeAll();
                        callpay(data.appId,data.timeStamp,data.nonceStr,data.package,data.signType,data.paySign);
                    }else{
                        layer.closeAll();
                        alert(data.msg);
                    }
                    
                }, 'json');
            });

        })

    </script>
